package com.github.wxpay.sdk;

import com.cn.tianxin.ssserver.utils.PropUtil;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class WXPayConfigImpl extends WXPayConfig {

    private byte[] certData;
    private static WXPayConfigImpl INSTANCE;

    private WXPayConfigImpl(String certPath) throws Exception {
        File file = new File(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

    private static class WXPayConfigImplHolder{
        private static WXPayConfigImpl wxPayConfig;
        private static PropUtil propUtil;
        static {
            try {
                propUtil = PropUtil.getPropUtilFromMap("weixin.properties");

                //默认获取证书路径
                wxPayConfig = new WXPayConfigImpl(propUtil.get("certPath"));    //从配置文件获取
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static WXPayConfigImpl getInstance(){
        INSTANCE = WXPayConfigImplHolder.wxPayConfig;
        return INSTANCE;
    }


    public String getAppID() {
        return WXPayConfigImplHolder.propUtil.get("appId");
    }

    public String getMchID() {
        return WXPayConfigImplHolder.propUtil.get("mchId");
    }

    public String getKey() {
        return WXPayConfigImplHolder.propUtil.get("key");
    }

    public InputStream getCertStream() {
        ByteArrayInputStream certBis;
        certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    IWXPayDomain getWXPayDomain() {
        return WXPayDomainSimpleImpl.instance();
    }



}
